<template>
	<view class="login-container">
		<!-- 项目logo目前没有 -->
		<!-- <img class="avatar"/> -->
		
		<view class="AppName">智慧研途</view>
		<view class="explain">———专注于您的研学旅途</view> 
		<view class="explain">提供一站式研学服务，助力研学旅行的深入发展</view>
		
		<button type="primary" @tap="userLogin" class="btnLogin">微信一键登录</button>
		<text class="tips-text">登录后享用更多权益</text>
		<label class="radio">
			<radio :value="toAgree" :checked="toAgree" @tap="toAgreeCondition" />
			<span @tap="toAgreeCondition">请阅读并同意
				<span class="userAgreement" @tap="toUserAgreement">用户协议</span>
				<span>、</span>
				<span class="privacyPolicy" @tap="toPrivacyPolicy">隐私条款</span>
			</span>
		</label>

	</view>
</template>

<script>
	import loginPageVue from './loginPage.vue';
	import {
		mapMutations
	} from 'vuex'
	export default {
		name: "loginPage",
		data() {
			return {
				userInfo: {
					userId: null
				},
				toAgree: false,
				isLogin:1
			};
		},
		methods: {
			...mapMutations('m_user', ['updateToken', 'updateUserId','updateUserInfo']),
			async userLogin() {
				if (this.toAgree == true) {
					const [err, res] = await uni.login().catch(err => err)
					if (res.errMsg !== "login:ok") return uni.showToast({
						title: "登录失败",
						icon: "error"
					})
					const query = {
						code: res.code
					}
					const loginResult = await uni.$u.http.post('/user/login', query)
					if (loginResult.errMsg !== "request:ok" && loginResult.data.code!==1) return uni.showToast({
						title: "登录失败",
						icon: "error"
					})
					this.updateToken(loginResult.data.data.token) //保存token
					this.updateUserId(loginResult.data.data.id) //保存 userId
					// 返回数据data.data{id,openid,token}
					const userInfo = await uni.$u.http.get("/user/detail/" + uni.getStorageSync("userId"), {
						custom: {
							auth: true
						}
					})
					this.updateUserInfo(userInfo)
					if (this.isLogin == 1) {
						uni.reLaunch({
							url:"/pages/home/home"
						 })//返回首页
					}else{
						uni.navigateBack()//跳回my.vue
					}
				} else {
					uni.showToast({
						title: "请同意条款",
						icon: "error"
					})
				}
			},
			toAgreeCondition() {
				this.toAgree = !this.toAgree
			},
			toUserAgreement() {
				uni.navigateTo({
					url: "/subpkg/useAgreement/useAgreement"
				});
			},
			toPrivacyPolicy() {
				uni.navigateTo({
					url: "/subpkg/privacyPolicy/privacyPolicy"
				});
			}
		}
	}
</script>

<style lang="scss">
	.login-container {
		height: 1200rpx;
		background-color: #f8f8f8;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		.AppName{
			margin-bottom: 45rpx;
			font-weight: 800;
			font-size: 50rpx;
		}
		.btnLogin {
			width: 90%;
			border-radius: 100px;
			margin: 30rpx 0;
			background-color: #058e73;
		}
		.explain{
			font-size: 26rpx;
			color: #bababa;
		}
		.tips-text {
			font-size: 12px;
			color: gray;
		}

		.avatar {
			width: 70px;
			height: 70px;
			border-radius: 45px;
			margin-bottom: 10px;
			box-shadow: 0 1px 5px black;
		}

		.userAgreement,
		.privacyPolicy {
			color: blue;
		}
	}
</style>